package com.zego.wawaji.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.bean.wawa.WawaJi;
import com.zego.wawaji.wawaji_client.OnItemClickListener;

import java.util.List;

/**
 * 娃娃机房间列表
 */
public class RoomListAdapter extends RecyclerView.Adapter<RoomListAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<WawaJi> mRoomList;
    private OnItemClickListener mItemClickListener;

    public RoomListAdapter(Context context, List<WawaJi> roomList) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        mRoomList = roomList;
    }

    public void setList(List<WawaJi> roomList) {
        this.mRoomList = roomList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_room_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WawaJi wawaji = mRoomList.get(position);
        ImageLoader.loadRoundTop(context, wawaji.getPicUrl(), holder.wawaImg);
        holder.tvRoomName.setText(wawaji.getNickName());
        holder.tvAward.setText(wawaji.getPrice() + "/次");
        holder.roomStatus.setBackgroundResource(getStatusRes(wawaji.getStatus()));
        holder.tvStatus.setText(getStatus(wawaji.getStatus()));
    }

    private int getStatusRes(int status) {
        int res = R.drawable.bg_circle_green;
        switch (status) {
            case 1:
                res = R.drawable.bg_circle_green;
                break;

            case -1:
                res = R.drawable.bg_circle_red;
                break;

            case -2:
                res = R.drawable.bg_circle_gray;
                break;
        }
        return res;
    }

    private String getStatus(int status) {
        String text = "空闲中";
        switch (status) {
            case 1:
                text = "空闲中";
                break;
            case -1:
                text = "游戏中";
                break;
            case -2:
                text = "休息中";
                break;
        }
        return text;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvRoomName, tvAward, tvStatus;
        private ImageView roomStatus, wawaImg;

        public ViewHolder(final View itemView) {
            super(itemView);
            tvRoomName = (TextView) itemView.findViewById(R.id.wawa_name);
            tvAward = (TextView) itemView.findViewById(R.id.tv_award);
            roomStatus = (ImageView) itemView.findViewById(R.id.room_status);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_status);
            wawaImg = (ImageView) itemView.findViewById(R.id.wawaji_img);

            itemView.findViewById(R.id.cv_item).setOnClickListener(view -> {
                if (mItemClickListener != null) {
                    mItemClickListener.onClick(itemView, getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mRoomList == null ? 0 : mRoomList.size();
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }
}
